package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Together;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.TogetherQueryObject;
import cn.wolfcode.trip.base.service.ITogetherService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 结伴游接口
 */
@RestController
@RequestMapping("togethers")
public class TogetherController {
    @Autowired
    ITogetherService togetherService;

    /**
     * 结伴游分页列表
     * @param qo
     * @return
     */
    @GetMapping
    public PageInfo list(TogetherQueryObject qo){
        if(qo.getFlag() == 1){
            qo.setOrderBy("t.releaseTime desc");
        }else if(qo.getFlag() == 2){
            qo.setOrderBy("t.travelTime");
        }
        PageInfo pageInfo = togetherService.query(qo);
        return pageInfo;
    }

    /**
     * 新增结伴游
     * @param together
     * @return
     */
    @PostMapping
    public JSONResult save(Together together){
        User user = UserContext.getCurrentUser();
        together.setAuthor(user);
        togetherService.save(together);
        return new JSONResult();
    }

    /**
     * 根据id  查询 结伴游
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Together getTogether(@PathVariable Long id){
        Together together = togetherService.selectTogetherById(id);
        return together;
    }

    /**
     * 根据togetherId 查询所有关注用户
     * @param togetherId
     * @return
     */
    @GetMapping("{togetherId}/users")
    public List<User> getUserByTogetherId(@PathVariable Long togetherId){
        List<User> users = togetherService.getUserByTogetherId(togetherId);
        return users;
    }

    /**
     * 查询用户与 结伴游之间有无关注关系
     * @param togetherId
     * @param userId
     * @return
     */
    @GetMapping("{togetherId}/{userId}")
    public Boolean getUser(@PathVariable Long togetherId,@PathVariable Long userId){
         Boolean b= togetherService.haveUser(togetherId,userId);
        return b;
    }
    /**
     * 取消关注
     * @param togetherId
     * @param userId
     * @return
     */
    @DeleteMapping("{togetherId}/{userId}")
    public JSONResult deleteFocus(@PathVariable Long togetherId,@PathVariable Long userId){
        togetherService.deleteFocus(togetherId,userId);
        return new JSONResult();
    }

    /**
     * 关注
     * @param togetherId
     * @param userId
     * @return
     */
    @PostMapping("{togetherId}/{userId}")
    public JSONResult saveFocus(@PathVariable Long togetherId,@PathVariable Long userId){
        togetherService.saveFocus(togetherId,userId);
        return new JSONResult();
    }
}
